<?php
class ModelMessageAdvice extends Model {
	
	//添加回复
	public function addReply($advice_id,$data = array()){
		$this->db->query("insert into ".DB_PREFIX."config_advice_reply set content='".$data['content']."',advice_id='".(int)$advice_id."'");
	}
	
	public function getAllAdviceReply($advice_id){
		$sql = "SELECT * FROM `" . DB_PREFIX . "config_advice_reply`  where advice_id='".(int)$advice_id."'";
		return $this->db->query($sql)->rows;
	}
	
public function getTotalAdvices() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "config_advice`");
		return $query->row['total'];
	}
	
	private function filterSql($data){
		$sql ="";
		
		if(isset($data['filter_title'])&&!empty($data['filter_title'])){
			$sql .= " and title like '%".$data['filter_title']."%'";
		}
		
		if(isset($data['filter_news_id'])&&!empty($data['filter_news_id'])){
			$sql .= " and news_id='".(int)$data['filter_news_id']."'";
		}
		
		return $sql;
		
	}
	
	public function getAdvices($data = array()) {
		$sql = "SELECT * FROM `" . DB_PREFIX . "config_advice`  where 1=1";

		$sql .=$this->filterSql($data);
		
		$sort_data = array(
			'date_added'
		);	
			
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			
			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
			
		$query = $this->db->query($sql);
	
		return $query->rows;
	}
	
	public function getAdvice($advice_id){
		$sql = "SELECT * FROM `" . DB_PREFIX . "config_advice`  where advice_id='".(int)$advice_id."'";
		return $this->db->query($sql)->row;
	}
	
	public function deleteAdvice($advice_id){
		$sql ="delete  from ".DB_PREFIX."config_advice where advice_id='".(int)$advice_id."'";
		$this->db->query($sql);
	}
}
?>